package controllers

import (
	"encoding/json"
	//"fmt"
	"strconv"
	"strings"
	//"strconv"
	"time"
	"yeoman/models"
	//"github.com/astaxie/beego"
)

// Operations about Article
type ArticleController struct {
	commonController
	//beego.Controller
}

// @Title 获取文章列表
// @Description 通过id获取文章详情
// @Param	id		path 	string	true
// @Param	page		path 	string	true
// @Param	psize		path 	string	true
// @Param	sort		path 	string	true
// @Param	order		path 	string	true
// @Success 200 {object} models.Article
// @Failure 403 id is empty
// @router /index [get]
func (this *ArticleController) Index() {
	this.AllowCross() //允许跨域
	typeId, _ := this.GetInt64("id", 0)
	page, _ := this.GetInt64("page")
	psize, _ := this.GetInt64("psize")
	sort := this.GetString("sort")
	order := this.GetString("order")
	_typeId := strconv.FormatInt(typeId, 10)
	_page := strconv.FormatInt(page, 10)
	_psize := strconv.FormatInt(psize, 10)
	cacheKey := strings.Join([]string{"index", _typeId, _page, _psize, sort, order}, "_")

	if this.cache.IsExist(cacheKey) {
		data := this.cache.Get(cacheKey)
		this.Data["json"] = data
		this.ServeJSON()
		return
	} else {
		if len(order) > 0 {
			if order == "desc" {
				sort = "-" + sort
			}
		} else {
			sort = "Id"
		}
		article, count := models.GetArticleList(typeId, page, psize, sort)
		data := &map[string]interface{}{"total": count, "rows": &article}
		//缓存10分钟
		this.cache.Put(cacheKey, data, 600*time.Second)

		this.Data["json"] = data
		this.ServeJSON()
		return
	}
}

// @Title 获取热门文章列表
// @Description 获取热门文章
// @Success 200 {object} models.Article
// @Failure 403 id is empty
// @router /hotlist [get]
func (this *ArticleController) HotList() {
	this.AllowCross() //允许跨域
	cacheKey := "blog_art_hot_list"
	if this.cache.IsExist(cacheKey) {
		data := this.cache.Get(cacheKey)
		this.Data["json"] = data
		this.ServeJSON()
		return
	} else {
		article := models.GetHotList()
		//缓存10分钟
		this.cache.Put(cacheKey, article, 600*time.Second)
		this.Data["json"] = &article
		this.ServeJSON()
		return
	}
}

// @Title 新建文章
// @Description 创建文章
// @Param	body		body 	models.Article	true		"body for article content"
// @Success 200 {int} models.Article.Id
// @Failure 403 body is empty
// @router /create [post]
func (this *ArticleController) Create() {
	article := models.Article{}
	json.Unmarshal(this.Ctx.Input.RequestBody, &article)
	id, err := models.AddArticle(&article)

	if err == nil && id > 0 {
		this.Rsp(1, "ok")
		return
	} else {
		this.Rsp(0, err.Error())
		return
	}
}

// @Title 更新文章
// @Description 更新文章
// @Param	body		body 	models.Article	true		"body for article content"
// @Success 200 {int} models.Article.Id
// @Failure 403 body is empty
// @router /update [post]
func (this *ArticleController) Update() {
	article := models.Article{}
	json.Unmarshal(this.Ctx.Input.RequestBody, &article)
	id, err := models.UpdateArticle(&article)
	if err == nil && id > 0 {
		this.Rsp(0, "ok")
		return
	} else {
		this.Rsp(1, err.Error())
		return
	}
}

// @Title 获取文章详情
// @Description 通过id获取文章详情
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Article
// @Failure 403 id is empty
// @router /detail [get]
func (this *ArticleController) GetDetail() {
	this.AllowCross() //允许跨域
	Id, _ := this.GetInt64("id")

	_Id := strconv.FormatInt(Id, 10)
	cacheKey := strings.Join([]string{"art_detail", _Id}, "_")

	if this.cache.IsExist(cacheKey) {
		data := this.cache.Get(cacheKey)
		this.Data["json"] = data
		this.ServeJSON()
		return
	} else {
		data := models.GetArticleById(Id)
		//缓存10分钟
		this.cache.Put(cacheKey, data, 600*time.Second)
		//data.CreateTime = strconv.FormatInt(data.CreateTime, 10)
		this.Data["json"] = &map[string]interface{}{"status": 0, "data": &data}
		this.ServeJSON()
		return
	}

}

// @Title 获取分类
// @Description 获取分类
// @Success 200 {object} models.ArticleType
// @Failure 403 not allow
// @router /typelist [get]
func (this *ArticleController) TypeList() {
	this.AllowCross() //允许跨域

	cacheKey := "blog_art_type_list"
	if this.cache.IsExist(cacheKey) {
		data := this.cache.Get(cacheKey)
		this.Data["json"] = data
		this.ServeJSON()
		return
	} else {
		artType := models.GetArtTypeList()
		//缓存10分钟
		this.cache.Put(cacheKey, artType, 600*time.Second)
		this.Data["json"] = &artType
		this.ServeJSON()
		return
	}

}
